package net.zoneland.geological.devicedata.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.zoneland.geological.devicedata.entity.Tbl_deep_move_device_data;
import net.zoneland.geological.devicedata.entity.Tbl_flow_device_data;
import net.zoneland.geological.devicedata.entity.Tbl_pressure_device_data;
import net.zoneland.geological.devicedata.entity.Tbl_rain_device_data;
import net.zoneland.geological.devicedata.entity.Tbl_surface_move_device_data;
import net.zoneland.geological.devicedata.entity.Tbl_water_device_data;
import net.zoneland.geological.devicedata.service.Tbl_deep_move_device_dataServiceI;
import net.zoneland.geological.devicedata.service.Tbl_flow_device_dataServiceI;
import net.zoneland.geological.devicedata.service.Tbl_pressure_device_dataServiceI;
import net.zoneland.geological.devicedata.service.Tbl_rain_device_dataServiceI;
import net.zoneland.geological.devicedata.service.Tbl_surface_move_device_dataServiceI;
import net.zoneland.geological.devicedata.service.Tbl_water_device_dataServiceI;
import net.zoneland.geological.project.entity.Tbl_project;
import net.zoneland.geological.project.entity.Tbl_zht;
import net.zoneland.geological.project.service.Tbl_projectServiceI;
import net.zoneland.geological.project.service.Tbl_zhtServiceI;
import net.zoneland.geological.station.entity.Tbl_station_info;
import net.zoneland.geological.station.entity.Tbl_station_type;
import net.zoneland.geological.station.entity.Tbl_warning_info;
import net.zoneland.geological.station.service.Tbl_station_infoServiceI;
import net.zoneland.geological.station.service.Tbl_station_typeServiceI;
import net.zoneland.geological.station.service.Tbl_warning_infoServiceI;

import org.apache.commons.collections.map.LinkedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 外部查询自动采集数据
 * @author Qinpo
 *
 */
@Controller
@RequestMapping("dataQuery")
public class DataQueryController {
	
	private final static String STATION_TYPE_RAIN = "1";
	
	private final static String STATION_TYPE_WATER = "2";
	
	private final static String STATION_TYPE_FLOW = "11";
	
	private final static String STATION_TYPE_PRESSURE = "20";
	
	private final static String STATION_TYPE_SURFACE_MOVE = "14";
	
	private final static String STATION_TYPE_DEEP_MOVE = "5";

	@Autowired
	private Tbl_rain_device_dataServiceI tbl_rain_device_dataServiceI;
	
	@Autowired
	private Tbl_water_device_dataServiceI tbl_water_device_dataServiceI;
	
	@Autowired
	private Tbl_flow_device_dataServiceI tbl_flow_device_dataServiceI;
	
	@Autowired
	private Tbl_pressure_device_dataServiceI tbl_pressure_device_dataServiceI;
	
	@Autowired
	private Tbl_deep_move_device_dataServiceI tbl_deep_move_device_dataServiceI;
	
	@Autowired
	private Tbl_surface_move_device_dataServiceI tbl_surface_move_device_dataServiceI;
	
	@Autowired
	private Tbl_station_infoServiceI tbl_station_infoServiceI;
	
	@Autowired
	private Tbl_zhtServiceI tbl_zhtServiceI;
	
	@Autowired
	private Tbl_projectServiceI tbl_projectServiceI;
	
	@Autowired
	private Tbl_warning_infoServiceI tbl_warning_infoServiceI;
	
	@Autowired
	private Tbl_station_typeServiceI tbl_station_typeServiceI;
	
	@RequestMapping(value = "rain")
	@ResponseBody
	public List<Map<String, Object>> rain() throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		
		//查出所有雨量设备
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("station_type", STATION_TYPE_RAIN);
		List<Tbl_station_info> stationList = tbl_station_infoServiceI.getList(paramMap);
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		//查询每个雨量设备的最新数据
		for(Tbl_station_info station : stationList) {
			paramMap.clear();
			
			paramMap.put("device_id", station.getStation_id());
			paramMap.put( "orderColName", "observe_time");
		 	paramMap.put( "orderType", "DESC");
		 	paramMap.put("startRow", 0);
		 	paramMap.put("pageSize", 1);
		 	
		 	List<Tbl_rain_device_data> dataList = tbl_rain_device_dataServiceI.getList(paramMap);
		 	if (dataList != null && dataList.size() > 0) {
		 		//查询设备所属灾害体和项目
		 		paramMap.clear();
		 		paramMap.put("id", station.getZht_id());
			 	Tbl_zht zht = tbl_zhtServiceI.get(paramMap);
			 	
			 	paramMap.clear();
			 	paramMap.put("id", zht.getProject_id());
			 	Tbl_project project = tbl_projectServiceI.get(paramMap);
			 			
		 		Map<String, Object> data = new LinkedMap();
		 		data.put("project", zht.getName());
		 		data.put("location", project.getLocation());
		 		data.put("station", station.getStation_name());
		 		data.put("value", dataList.get(0).getDay_rain_value());
		 		data.put("warning_value", 50);
		 		data.put("time", df.format(dataList.get(0).getObserve_time()));
		 		
		 		list.add(data);
		 	}
		}
		
		return list;
	}
	
	@RequestMapping(value = "water")
	@ResponseBody
	public List<Map<String, Object>> water() throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		
		//查出所有雨量设备
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("station_type", STATION_TYPE_WATER);
		List<Tbl_station_info> stationList = tbl_station_infoServiceI.getList(paramMap);
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		//查询每个雨量设备的最新数据
		for(Tbl_station_info station : stationList) {
			paramMap.clear();
			
			paramMap.put("device_id", station.getStation_id());
			paramMap.put( "orderColName", "observe_time");
		 	paramMap.put( "orderType", "DESC");
		 	paramMap.put("startRow", 0);
		 	paramMap.put("pageSize", 1);
		 	
		 	List<Tbl_water_device_data> dataList = tbl_water_device_dataServiceI.getList(paramMap);
		 	if (dataList != null && dataList.size() > 0) {
		 		//查询设备所属灾害体和项目
		 		paramMap.clear();
		 		paramMap.put("id", station.getZht_id());
			 	Tbl_zht zht = tbl_zhtServiceI.get(paramMap);
			 	
			 	paramMap.clear();
			 	paramMap.put("id", zht.getProject_id());
			 	Tbl_project project = tbl_projectServiceI.get(paramMap);
			 			
		 		Map<String, Object> data = new LinkedMap();
		 		data.put("project", zht.getName());
		 		data.put("location", project.getLocation());
		 		data.put("station", station.getStation_name());
		 		data.put("value", dataList.get(0).getWater_value());
		 		data.put("warning_value", 50);
		 		data.put("time", df.format(dataList.get(0).getObserve_time()));
		 		
		 		list.add(data);
		 	}
		}
		
		return list;
	}
	
	@RequestMapping(value = "flow")
	@ResponseBody
	public List<Map<String, Object>> flow() throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		
		//查出所有雨量设备
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("station_type", STATION_TYPE_FLOW);
		List<Tbl_station_info> stationList = tbl_station_infoServiceI.getList(paramMap);
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		//查询每个雨量设备的最新数据
		for(Tbl_station_info station : stationList) {
			paramMap.clear();
			
			paramMap.put("device_id", station.getStation_id());
			paramMap.put( "orderColName", "observe_time");
		 	paramMap.put( "orderType", "DESC");
		 	paramMap.put("startRow", 0);
		 	paramMap.put("pageSize", 1);
		 	
		 	List<Tbl_flow_device_data> dataList = tbl_flow_device_dataServiceI.getList(paramMap);
		 	if (dataList != null && dataList.size() > 0) {
		 		//查询设备所属灾害体和项目
		 		paramMap.clear();
		 		paramMap.put("id", station.getZht_id());
			 	Tbl_zht zht = tbl_zhtServiceI.get(paramMap);
			 	
			 	paramMap.clear();
			 	paramMap.put("id", zht.getProject_id());
			 	Tbl_project project = tbl_projectServiceI.get(paramMap);
			 			
		 		Map<String, Object> data = new LinkedMap();
		 		data.put("project", zht.getName());
		 		data.put("location", project.getLocation());
		 		data.put("station", station.getStation_name());
		 		data.put("value", dataList.get(0).getFlow_value());
		 		data.put("warning_value", 50);
		 		data.put("time", df.format(dataList.get(0).getObserve_time()));
		 		
		 		list.add(data);
		 	}
		}
		
		return list;
	}
	
	@RequestMapping(value = "pressure")
	@ResponseBody
	public List<Map<String, Object>> pressure() throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		
		//查出所有雨量设备
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("station_type", STATION_TYPE_PRESSURE);
		List<Tbl_station_info> stationList = tbl_station_infoServiceI.getList(paramMap);
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		//查询每个雨量设备的最新数据
		for(Tbl_station_info station : stationList) {
			paramMap.clear();
			
			paramMap.put("device_id", station.getStation_id());
			paramMap.put( "orderColName", "observe_time");
		 	paramMap.put( "orderType", "DESC");
		 	paramMap.put("startRow", 0);
		 	paramMap.put("pageSize", 1);
		 	
		 	List<Tbl_pressure_device_data> dataList = tbl_pressure_device_dataServiceI.getList(paramMap);
		 	if (dataList != null && dataList.size() > 0) {
		 		//查询设备所属灾害体和项目
		 		paramMap.clear();
		 		paramMap.put("id", station.getZht_id());
			 	Tbl_zht zht = tbl_zhtServiceI.get(paramMap);
			 	
			 	paramMap.clear();
			 	paramMap.put("id", zht.getProject_id());
			 	Tbl_project project = tbl_projectServiceI.get(paramMap);
			 			
		 		Map<String, Object> data = new LinkedMap();
		 		data.put("project", zht.getName());
		 		data.put("location", project.getLocation());
		 		data.put("station", station.getStation_name());
		 		data.put("value", dataList.get(0).getKpa_value());
		 		data.put("warning_value", 50);
		 		data.put("time", df.format(dataList.get(0).getObserve_time()));
		 		
		 		list.add(data);
		 	}
		}
		
		return list;
	}
	
	@RequestMapping(value = "surfaceMove")
	@ResponseBody
	public List<Map<String, Object>> surfaceMove() throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		
		//查出所有雨量设备
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("station_type", STATION_TYPE_SURFACE_MOVE);
		List<Tbl_station_info> stationList = tbl_station_infoServiceI.getList(paramMap);
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		//查询每个雨量设备的最新数据
		for(Tbl_station_info station : stationList) {
			paramMap.clear();
			
			paramMap.put("device_id", station.getStation_id());
			paramMap.put( "orderColName", "observe_time");
		 	paramMap.put( "orderType", "DESC");
		 	paramMap.put("startRow", 0);
		 	paramMap.put("pageSize", 1);
		 	
		 	List<Tbl_surface_move_device_data> dataList = tbl_surface_move_device_dataServiceI.getList(paramMap);
		 	if (dataList != null && dataList.size() > 0) {
		 		//查询设备所属灾害体和项目
		 		paramMap.clear();
		 		paramMap.put("id", station.getZht_id());
			 	Tbl_zht zht = tbl_zhtServiceI.get(paramMap);
			 	
			 	paramMap.clear();
			 	paramMap.put("id", zht.getProject_id());
			 	Tbl_project project = tbl_projectServiceI.get(paramMap);
			 			
		 		Map<String, Object> data = new LinkedMap();
		 		data.put("project", zht.getName());
		 		data.put("location", project.getLocation());
		 		data.put("station", station.getStation_name());
		 		data.put("x_value", dataList.get(0).getX_move_value());
		 		data.put("y_value", dataList.get(0).getY_move_value());
		 		data.put("z_value", dataList.get(0).getZ_move_value());
		 		data.put("warning_value", 50);
		 		data.put("time", df.format(dataList.get(0).getObserve_time()));
		 		
		 		list.add(data);
		 	}
		}
		
		return list;
	}
	
	@RequestMapping(value = "deepMove")
	@ResponseBody
	public List<Map<String, Object>> deepMove() throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		
		//查出所有雨量设备
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("station_type", STATION_TYPE_DEEP_MOVE);
		List<Tbl_station_info> stationList = tbl_station_infoServiceI.getList(paramMap);
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		//查询每个雨量设备的最新数据
		for(Tbl_station_info station : stationList) {
			paramMap.clear();
			
			paramMap.put("device_id", station.getStation_id());
			paramMap.put( "orderColName", "observe_time");
		 	paramMap.put( "orderType", "DESC");
		 	paramMap.put("startRow", 0);
		 	paramMap.put("pageSize", 1);
		 	
			List<Tbl_deep_move_device_data> dataList = tbl_deep_move_device_dataServiceI.getList(paramMap);
		 	if (dataList != null && dataList.size() > 0) {
		 		//查询设备所属灾害体和项目
		 		paramMap.clear();
		 		paramMap.put("id", station.getZht_id());
			 	Tbl_zht zht = tbl_zhtServiceI.get(paramMap);
			 	
			 	paramMap.clear();
			 	paramMap.put("id", zht.getProject_id());
			 	Tbl_project project = tbl_projectServiceI.get(paramMap);
			 			
		 		Map<String, Object> data = new LinkedMap();
		 		data.put("project", zht.getName());
		 		data.put("location", project.getLocation());
		 		data.put("station", station.getStation_name());
		 		data.put("a_value", dataList.get(0).getA_move_value());
		 		data.put("b_value", dataList.get(0).getB_move_value());
		 		data.put("warning_value", 50);
		 		data.put("time", df.format(dataList.get(0).getObserve_time()));
		 		
		 		list.add(data);
		 	}
		}
		
		return list;
	}
	
	@RequestMapping(value = "warningInfo")
	@ResponseBody
	public List<Map<String, Object>> warningInfo() throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		
		//获取当前时间
		Date currentDate = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currentTime = df.format(currentDate);
		
		//查询当前时间小时内的数据
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("search_start_time", currentTime.substring(0, 14) + "00:00");
		paramMap.put("search_end_time", currentTime.substring(0, 14) + "59:59");
//		paramMap.put("search_start_time", "2017-11-10 16:00:00");
//		paramMap.put("search_end_time", "2017-11-10 16:59:59");
		
		List<Tbl_warning_info> warningInfoList = tbl_warning_infoServiceI.getList(paramMap);
		
		for (Tbl_warning_info warningInfo : warningInfoList) {
			Map<String, Object> data = new LinkedMap();
			
			paramMap.clear();
	 		paramMap.put("id", warningInfo.getZht_id());
		 	Tbl_zht zht = tbl_zhtServiceI.get(paramMap);
		 	
		 	paramMap.clear();
		 	paramMap.put("id", zht.getProject_id());
		 	Tbl_project project = tbl_projectServiceI.get(paramMap);
		 	
		 	paramMap.clear();
		 	paramMap.put("station_id", warningInfo.getStation_id());
		 	Tbl_station_info station = tbl_station_infoServiceI.get(paramMap);
		 	
		 	paramMap.clear();
		 	paramMap.put("id", station.getStation_type());
		 	Tbl_station_type stationType = tbl_station_typeServiceI.get(paramMap);
		 	
	 		data.put("project", zht.getName());
	 		data.put("location", project.getLocation());
	 		data.put("station_name", warningInfo.getStation_name());
	 		data.put("station_id", warningInfo.getStation_id());
	 		data.put("time", df.format(warningInfo.getCreate_time()));
	 		data.put("warn_level", warningInfo.getWarn_level());
	 		data.put("warn_option", warningInfo.getWarn_option());
	 		data.put("warn_value", warningInfo.getWarn_value());
	 		data.put("current_value", warningInfo.getCurrent_value());
	 		data.put("station_type", stationType.getType_name());
	 		
	 		list.add(data);
		}
		
		return list;
	}
	
	/*public static void main(String[] ser){
		Date currentDate = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currentTime = df.format(currentDate);
		
		System.out.println(currentTime.substring(0, 14) + "00:00");
		System.out.println(currentTime.substring(0, 14) + "59:59");
	}*/
}
